package com.nenglai.admin.modular.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nenglai.admin.modular.report.enums.UploadServiceEnum;
import com.nenglai.admin.modular.report.form.TemplateForm;
import com.nenglai.admin.modular.report.util.ReportUtil;
import com.nenglai.admin.modular.report.vo.ReportTemplateVO;
import com.nenglai.common.crud.EditorBody;
import com.nenglai.common.crud.FormBody;
import com.nenglai.common.crud.RemoveBody;
import com.nenglai.common.crud.SearchBody;
import com.nenglai.common.enums.EnumUtils;
import com.nenglai.common.resource.Datalist;
import com.nenglai.common.resource.ResourceService;
import com.nenglai.common.response.ErrorCode;
import com.nenglai.common.response.ResponseException;
import com.nenglai.common.response.ValidateException;
import com.nenglai.common.vo.SearchOneVO;
import com.nenglai.common.vo.SearchVO;
import com.nenglai.model.report.entity.Template;
import com.nenglai.model.report.service.ITemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kongdehui
 * @date 2021/06/18 15:26
 **/
@Slf4j
@Service
public class ReportTemplateService implements ResourceService<ReportTemplateVO> {

    @Resource
    ITemplateService templateService;

    @Resource
    ReportUtil reportUtil;

    @Value("${report.template-path}")
    private String reportTemplatePath;

    @Override
    public String resourceName() {
        return "report-template";
    }

    @Override
    @Datalist(entity = Template.class, vo = ReportTemplateVO.class)
    public SearchVO<Template> search(SearchBody body) {
        QueryWrapper<Template> query = body.query(new QueryWrapper<>(), Template.class);
        Page<Template> page = templateService.page(new Page<>(body.getPage(), body.getPagesize()), query);
        return new SearchVO<>(page.getRecords(), page.getTotal());
    }

    @Override
    public SearchOneVO<ReportTemplateVO> searchOne(SearchBody body) {
        return null;
    }

    @Override
    public Boolean delete(RemoveBody body) {
        QueryWrapper<Template> query = body.query(new QueryWrapper<>(), Template.class, true);
        Template template = templateService.getOne(query);
        if (template == null) {
            throw new ResponseException(ErrorCode.OBJECT_DOES_NOT_EXIST);
        }
        reportUtil.clearFile(template.getService(), template.getFilePath());
        return templateService.remove(query);
    }

    @Override
    public Object add(FormBody body) throws ValidateException {
        return null;
    }

    public boolean add(MultipartFile file, TemplateForm form) {
        Template template = new Template();
        BeanUtils.copyProperties(form, template);
        try {
            template.setService(form.getUploadService());
            String uploadTemplate = reportUtil.uploadTemplate(file, form.getUploadService(),
                    form.getUploadPath(reportTemplatePath), IdWorker.get32UUID());
            template.setFilePath(uploadTemplate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ResponseException(ErrorCode.REPORT_TEMPLATE_UPLOAD_FAIL);
        }
        return templateService.save(template);
    }

    @Override
    public Object update(FormBody body) throws ValidateException {
        return null;
    }

    public boolean update(MultipartFile file, TemplateForm form) {
        Template template = templateService.getById(form.getId());
        if (template == null) {
            throw new ResponseException(ErrorCode.OBJECT_DOES_NOT_EXIST);
        }
        String oldService = template.getService();
        String oldFilePath = template.getFilePath();
        BeanUtils.copyProperties(form, template);
        if (file != null) {
            try {
                template.setService(form.getUploadService());
                String uploadTemplate = reportUtil.uploadTemplate(file, form.getUploadService(),
                        form.getUploadPath(reportTemplatePath), IdWorker.get32UUID());
                template.setFilePath(uploadTemplate);
                reportUtil.clearFile(oldService, oldFilePath);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ResponseException(ErrorCode.REPORT_TEMPLATE_UPLOAD_FAIL);
            }
        }
        return templateService.updateById(template);
    }

    @Override
    public Boolean edit(EditorBody body) throws ValidateException {
        UpdateWrapper<Template> update = body.query(new UpdateWrapper<>(), Template.class, true);
        return templateService.update(update);
    }

    @Override
    public Object configs() {
        Map<String, Object> hash = new HashMap<>();
        hash.put("upload-service", EnumUtils.EnumToMapInt(UploadServiceEnum.class));
        return hash;
    }
}
